package com.longcai.piaojia.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.longcai.piaojia.R;
import com.longcai.piaojia.conn.ProjectListGet;
import com.longcai.piaojia.conn.ShowListGet;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.glide.GlideLoader;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.List;

/**
 * Created by Administrator on 2016/11/7.
 */
public class ProjectListAdapter extends ArrayAdapter<ProjectListGet.Project> {

    private LayoutInflater layoutInflater;
    private Context context;

    public ProjectListAdapter(Context context, List<ProjectListGet.Project> objects) {
        super(context, 0, objects);

        layoutInflater = LayoutInflater.from(context);

        this.context = context;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


        ViewHolder viewHolder;

        ProjectListGet.Project project = getItem(position);

        if (convertView == null){

            convertView = BoundViewHelper.boundView(viewHolder = new ViewHolder(), ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) layoutInflater.inflate(R.layout.item_project_list, null)));

            convertView.setTag(viewHolder);

        } else {

            viewHolder = (ViewHolder) convertView.getTag();
        }

        GlideLoader.getInstance().get(context, project.picurl, viewHolder.item_project_list_iv, R.mipmap.home_center, GlideLoader.TYPE_IMAGE_FILLET);


        viewHolder.item_project_list_title_tv.setText(project.title);
        viewHolder.item_project_list_introduce_tv.setText(project.intro);
        viewHolder.item_project_list_price_tv.setText(project.price);
        viewHolder.item_project_list_starttime_tv.setText(project.starttime + context.getString(R.string.goods_info_symbols_text) + project.endtime);

        if (project.step.equals("2")) {

            viewHolder.item_project_list_state_iv.setImageResource(R.mipmap.project_ongoing_icon);

        } else if (project.step.equals("3")){

            viewHolder.item_project_list_state_iv.setImageResource(R.mipmap.project_over_icon);

        } else {

            viewHolder.item_project_list_state_iv.setImageResource(R.mipmap.project_nostart_icon);

        }


        return convertView;
    }

    class ViewHolder {


        @BoundView(R.id.item_project_list_iv) private ImageView item_project_list_iv;
        @BoundView(R.id.item_project_list_title_tv) private TextView item_project_list_title_tv;
        @BoundView(R.id.item_project_list_introduce_tv) private TextView item_project_list_introduce_tv;
        @BoundView(R.id.item_project_list_price_tv) private TextView item_project_list_price_tv;
        @BoundView(R.id.item_project_list_starttime_tv) private TextView item_project_list_starttime_tv;
        @BoundView(R.id.item_project_list_state_iv) private ImageView item_project_list_state_iv;

    }
}
